<?php

namespace app\api\model;

use app\api\controller\Pay;
use app\common\exception\Parameter;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

class Follow extends Model
{
    // 设置软删除字段
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    // 关联用户模型
    public function user()
    {
        return $this->belongsTo(User::class, 'follow_id', 'id');
    }
    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public static function toggleFollow($userId, $targetId)
    {
        if ($userId == $targetId) {
            throw new Parameter(['msg' => '不能关注自己哦']);
        }
        $follow = Db::name('follow')
            ->where('user_id', $userId)
            ->where('follow_id', $targetId)
            ->where('relation_type', 1)
            ->find();
        if (!$follow) {
            $exist = User::find($targetId);
            if (!$exist)  throw new Parameter(['msg' => '用户不存在']);
            // 不存在则创建新关注
            $res =  Db::name('follow')->insertGetId([
                'user_id' => $userId,
                'follow_id' => $targetId,
                'relation_type' => 1,
                'create_time' => time(),
            ]);
        } else {
            // 存在则切换软删除状态
            $deleteTime = $follow['delete_time']  ? 0 : time();
            $res =  Db::name('follow')
                ->where('user_id', $userId)
                ->where('follow_id', $targetId)
                ->where('relation_type', 1)
                ->update([
                    'delete_time' => $deleteTime,
                    'update_time' => time()
                ]);
        }
        return $res;
    }

    public static function getMyFollows($userId)
    {
        // 使用模型查询
        $follows = self::alias('f')
            ->join('user u', 'f.follow_id = u.id')
            ->where('f.user_id', $userId)
            ->where('f.delete_time', 0)
            ->where('f.relation_type', 1)
            ->field([
                'u.id',
                'u.nickname',
                'u.avator',
                'u.last_time',  // 添加最近在线时间字段
            ])
            ->order('f.create_time', 'desc')
            ->paginate();
        $items = $follows->getCollection();
        foreach ($items as &$follow) {
            $follow['last_time'] = formatMessageTime($follow['last_time']);
            $follow['avator'] = addHostForImage($follow['avator']);
        }
        $follows->setCollection($items);
        return $follows;
    }
    public static function checkIsFriend($friend_id)
    {
        $isFriend = Follow::alias('f1')
            ->whereExists(function ($query) use ($friend_id) {
                $query->table('gn_follow')
                    ->alias('f2')
                    ->whereColumn('f2.user_id', 'f1.follow_id')
                    ->whereColumn('f2.follow_id', 'f1.user_id')
                    ->where('f2.relation_type', 2)
                    ->where('f2.delete_time', 0);
            })
            ->where([
                ['f1.user_id', '=', getUid()],
                ['f1.follow_id', '=', $friend_id],
                ['f1.relation_type', '=', 2],
                ['f1.delete_time', '=', 0]
            ])
            ->find();
        return $isFriend;
    }
}
